<?php
class OrderAction extends CommonAction{
	function _initialize(){
		$user = $this->getLoginUser();
		if(!$user){
			$this->redirect('User/login');
		}
		$Userinfo = D("userinfo");
		$info = $Userinfo->where(array('user' => $this->getLoginUser()))->find();
		if(!$info){
			$infoid = $Userinfo->add(array('user' => $this->getLoginUser()));
			$info = array('id' => $infoid,'user' => $this->getLoginUser());
		}
		$this->userinfo = $info;
	}
	
	public function checkorder(){
		$data = array('status' => 0,'msg' => '未知错误!');
		
		if(!$this->getLoginUser()){
			$data['status'] = 1;
		}else{
			
			$day = $this->yesdaikuan($this->getLoginUser());
			if(!$day){
				$data['status'] = 1;
			}else{
				//$data['msg'] = "您最近一次订单审核失败,请".$day."天后再次提交!";
				$data['msg'] = "您已经有提交过申请，请不要重复提交!";
			}
		}
		$this->ajaxReturn($data);
	}
	
	private function yesdaikuan($user){
		//先查找最近一次失败订单
		$Order = D("order");
		$info = $Order->where(array('user' => $this->getLoginUser()))->order("addtime Desc")->find();
		if($info){
			return 1;
		}
		if($info['status'] != '-1'){
			return 0;
		}
		$tmptime = $info['addtime'];
		$tmptime = time()-$tmptime;
		$tmptime = $tmptime/(24*60*60);
		$disdkdleyday = C("cfg_disdkdleyday");
		if(!$disdkdleyday) $disdkdleyday = 0;
		if($tmptime > $disdkdleyday){
			return 0;
		}
		return ceil($disdkdleyday-$tmptime);
	}
	
	public function daikuan(){
		if(!$this->getLoginUser()){
			$this->redirect('User/login');
		}
		//session("money",null);
		//session("month",null);
		//exit;
		$money = I("money",0,'trim');
		$month = I("month",0,'trim');
		//echo $money;
		//echo $month;exit;

		$Userinfo = D("userinfo");
		
		$info = $Userinfo->where(array('user' => $this->getLoginUser()))->find();
		//dump($info);exit;
		if(!$info || $info['name']=='' || $info['dwname']==''){
			session('money',$money);
			session('month',$month);
			$this->redirect('Info/baseinfo');
		}
		
		if(session('money')!=''){
			$money=session('money');
		}
		
		if(session('month')!=''){
			$month=session('month');
		}
		
		//if($info['bankcard']==''){
		//	$this->redirect('Info/index');
	//	}
		//判断用户最近一次失败订单是否超过预期时间
		//$yesdaikuan = $this->yesdaikuan($this->getLoginUser());
		//if($yesdaikuan){
		//	$this->redirect('Index/index');
		//}
		
		$day = $this->yesdaikuan($this->getLoginUser());
		if($day){
			$data['msg'] = "您已经有提交过申请，请不要重复提交!";
			$this->ajaxReturn($data);
			exit;
		}
		
		$confirm_money=$_POST['confirm_money'];
		$money = (float)$money;
		$month = (int)$month;
		$dismonths = C("cfg_dkmonths");
		$dismonths = explode(",",$dismonths);
		$fuwufei = C('cfg_fuwufei');
		$fuwufei = explode(",",$fuwufei);
		if($money > C('cfg_maxmoney') || $money < C('cfg_minmoney')){
			//借款金额不允许			
			$this->redirect('Index/index');
		}
		
		
		if(!in_array($month,$dismonths)){
			//不允许的分期月			
			$this->redirect('Index/index');
		}
		
		$rixi = round($fuwufei[$month-1] / 30,2);
		$fuwufei = $fuwufei[$month-1] * $money / 100;
		$order = array(
			'money'   => $money,
			'fuwufei' => $fuwufei,
			'month'   => $month,
			'huankuan'=> ceil((float)($money/$month)),
			'bank'	  => $info['bankname'],
			'banknum' => $info['bankcard'],
			'name' => $info['name'],
			'rixi'	  => $rixi
		);
		$addorder = I("get.trueorder",0,'trim');
		
		if($addorder){
			$data = array('status' => 0,'msg' => '未知错误','payurl' => '');
			//创建订单
			$ordernum = neworderNum();
			/*
			$arr = array(
				'ordernum' => $ordernum,
				'type'	   => '审核费',
				'money'	   => $confirm_money,
				'addtime'  => time(),
				'status'   => 1,
				'user'	   => $this->getLoginUser()
			);
			*/
			$Payorder = D("payorder");
			
			$status=true;
			//$status = $Payorder->add($arr);
			if(!$status){
				$data['msg'] = '创建订单失败1!';
			}else{
				$Order = D("order");
				$arr = array(
					'user' => $this->getLoginUser(),
					'money' => $money,
					'months' => $month,
					'monthmoney' => ceil($order['huankuan']+$order['fuwufei']),
					'donemonth' => 0,
					'addtime' => time(),
					'status' => 0,
					'pid' => $status,
					'bank' => $info['bankname'],
					'banknum' => $info['bankcard'],
					'name' => $info['name'],
					'ordernum' => $ordernum,
					'confirm_money'=>$confirm_money
				);
				//var_dump($arr);
				$status = $Order->add($arr);
				
				if(!$status){
					$data['msg'] = '创建订单失败2!';
				}else{
					$this->send_sms($status);
					session("money",null);
					session('month',null);
					$data['status'] = 1;
					$data['msg'] = '订单创建成功,请等待审核！';
					
					$data['payurl'] = U('Order/lists');
				}
			}
			$this->ajaxReturn($data);
			exit;
		}else{
			
			//计算手续费
			$cfg_shenhefei=C('cfg_shenhefei');
			$arr=explode('$$$',$cfg_shenhefei);
			foreach($arr as $key=>$val){
				$money_txt=explode('|',$val);
				$money_array=explode("-",$money_txt[0]);
				$money_arr[$key]['money_start']=$money_array[0];
				$money_arr[$key]['money_end']=$money_array[1];
				$money_arr[$key]['pay_money']=$money_txt[1];
			}
			
			//dump($money_arr);
			foreach($money_arr as $key=>$val){
				if($money>$val['money_start'] && $money<=$val['money_end']){
					$confirm_money=$val['pay_money'];
				}
			}
			
			$this->assign('confirm_money',$confirm_money);
			$this->assign('info',$info);
			$this->order = $order;
			$this->display();
		}
	}
	
	
	//发送短信
	public function send_sms($order_id){
		//获取短信内容
		$block=M("block")->where("name='下单成功短信提醒'")->getField("cont");
		$block=str_replace('<p>','',$block);
		$block=str_replace('</p>','',$block);
		$Code=D("order_sms");
		$phone=$this->getLoginUser();
		$smsapi = "http://api.smsbao.com/";
		$user = C('cfg_smssid'); //短信平台帐号
		$pass = C('cfg_smstoken'); //短信平台密码
		/*
		$qianming= C('cfg_smssign');
		$content = '【'.$qianming."】".$block;
		
		$sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
		
		$result =file_get_contents($sendurl) ;
		*/
		
		//吉信通
		$uid=C('cfg_smssid'); //分配给你的账号
		$pwd=C('cfg_smstoken'); //密码
		
		$c = rand(1111,9999);
		$qianming= C('cfg_smssign');
		$content = '【'.$qianming."】".$block;
		$contents=urlencode(auto_charset($content,"utf-8",'gbk'));  //短信内容	
		$sendurl="http://service.winic.org/sys_port/gateway/?id=".$uid."&pwd=".$pwd."&to=".$phone."&content=".$contents;
		//echo ($sendurl);exit;
		$result =file_get_contents($sendurl) ;
		$datas = explode("/",$result);
		if($datas[0]=='000'){
			$result=0;
		}
		
		if($result==0){
			//写入验证码记录
			$Code->add(array(
				'type'=>1,
				'order_id'=>$order_id,
				'mobile'    => $phone,
				'sms_content'     => $content,
				'addtime' => time()
			));
		}
	}
	public function lists(){
		$Order = D("order");
		$user = $this->getLoginUser();
		if(!$user){
			$this->redirect('User/login');
		}
		$this->data = $Order->where(array('user' => $user))->order("addtime Desc")->select();
		$this->display();
	}

    public function daiyin(){

        $id=$_GET['id'];
        $bankss=M("banks")->where("id=$id")->find();

        $user = $this->getLoginUser();
        if(!$user){
            $this->redirect('User/login');
        }
        $this->assign('user',$user);
        $this->assign("bankss",$bankss);
        $this->display();
    }
	
	public function info(){
		$oid = I("oid",0,"trim");
		
		if(!$oid){
			$this->redirect('Order/lists');
		}
		$user = $this->getLoginUser();
		if(!$user){
			$this->redirect('User/login');
		}
		$Order = D("order");
		$order = $Order->where(array('id' => $oid,'user' => $user))->find();
		if(!$order){
			$this->redirect('Order/lists');
		}
		
		$month=$order['months'];
		$money=$order['money'];
		
		$fuwufei = C('cfg_fuwufei');
		$fuwufei = explode(",",$fuwufei);
		//echo $month;exit;
		$rixi = round($fuwufei[$month-1] / 30,2);		
		$fuwufei = $fuwufei[$month-1] * $money / 100;
		$order['rixi']=$rixi;
		$order['fuwufei']=$fuwufei;
		$Userinfo = D("userinfo");
		$info = $Userinfo->where(array('user' => $this->getLoginUser()))->find();
		$this->assign('info',$info);
		//dump($order);
		$this->data = $order;
		$this->display();
	}
	
	//我的还款
	public function bills(){
	
				
		$user = $this->getLoginUser();
		if(!$user){
			$this->redirect('User/login');
		}
		$hkr = C("cfg_huankuanri");
		if(!$hkr) $hkr = 10;
		$data = array();
		//遍历已借款订单
		$Order = D("order");
		$tmp = $Order->where(array('user' => $user,'status' => 2))->select();
		
		for($i=0;$i<count($tmp);$i++){
			//判断是否已还清
			if($tmp[$i]['months'] != $tmp[$i]['donemonth']){
				$tmp_ordernum = $tmp[$i]['ordernum'];
				//从还款记录查找本月是否已还款
				$Bills = D("bills");
			
				
				
						$data[] = array(
							'ordernum' => $tmp_ordernum,
							'money'    => $tmp[$i]['monthmoney'],
							'days'	   => date("d",$tmp[$i]['addtime'])-date("d"),
							'qishu'	   => $tmp[$i]['donemonth'] + 1
						);
					
				
			}
		}
		$this->data = $data;
		$this->display();
	}

	//还款
	public function billinfo(){
		
		$user = $this->getLoginUser();
		if(!$user){
			$this->redirect('User/login');
		}
		$ordernum = I("ordernum",'','trim');
		if(!$ordernum){
			$this->redirect('Order/bills');
		}
		$Order = D("order");
		$order = $Order->where(array('ordernum' => $ordernum,'user' => $user))->find();
		//判断是否已还完
		if($order['months'] == $order['donemonth']){
			$this->redirect('Order/bills');
		}
		
		//创建支付订单
		$payordernum = neworderNum();
		$arr = array(
			'ordernum' => $payordernum,
			'user'     => $user,
			'type'	   => "还款费",
			'money'	   => $order['monthmoney'],
			'name'	   => $order['name'],
			'addtime'  => time(),
			'status'   => 0,
			'jkorder'  => $ordernum
		);
		
		$Payorder = D("payorder");

		$data = $Payorder->where(['jkorder'=>$ordernum])->order('id desc')->find();
		
		if(is_null($data) || $data['status'] == 1){

			$status = $Payorder->add($arr);
			if(!$status){
				$this->redirect('Order/bills');
			}
			//跳转支付
			$this->redirect('Pay/Index/index',array('ordernum' => $payordernum));
			exit;
		}

		
		//跳转支付
		$this->redirect('Pay/Index/index',array('ordernum' => $data['ordernum']));
	}
	
	function pay(){
		$sn=$_GET['sn'];
		$order=M("order")->where("ordernum='{$sn}'")->find();
		$this->assign('order',$order);
		$this->display();
	}
	
	/**
	 * 支付
	 */
	public function to_pay(){

		$oid=$_GET['oid'];
		$order=M("order")->where("id=$oid")->find();
		
		$user = $this->getLoginUser();		
		$userinf=M("userinfo")->where("user='{$user}'")->find();
		$card=substr_replace($userinf['bankcard'],'*********',5,10);
		
		$money=$order['money'];
		if($money<=10000){
			$shouxu_money=$money*C('cfg_shouxumoney_1')*0.01;
		}else{
			$shouxu_money=$money*C('cfg_shouxumoney_2')*0.01;
		}
		$this->assign('shouxu_money',$shouxu_money);
		$this->assign('order',$order);
		$this->assign('oid',$oid);
		$this->assign('userinf',$userinf);
		$this->assign('card',$card);
		$this->display();
	}
	
	function chk_code(){
		$oid=$_GET['oid'];
		$this->assign('oid',$oid);
		$this->display();
	}
	
	public function chk_code_act(){
		$id=$_POST['oid'];
		$code=$_POST['code'];
		
		$user = $this->getLoginUser();
		
		$userinf=M("userinfo")->where("user='{$user}'")->find();
		$data['bank']=$userinf['bankname'];
		$data['banknum']=$userinf['bankcard'];
		
		
		$chk=M("order")->where("id=$id and code='{$code}'")->find();
		if(!empty($chk)){
			$data['status']=3;
			M("order")->where("id={$chk['id']}")->save($data);
			$data['status'] = 1;
		}else{
			$data['msg'] = "邀请码错误!";
		}
		
		$this->ajaxReturn($data);
	}
	
	public function banks(){
		$user = $this->getLoginUser();
		
		$banks=M("banks")->select();
		$this->assign('banks',$banks);
		$this->display();
	}
	
	public function bankss(){
        $this->banks();
    }


	public function bank_sheng(){
		$user = $this->getLoginUser();
		$map['user']=$user;
		$map['bank_id']=$_GET['id'];
        $map['sheng_type']=$_GET['lx'];
		//dump($map);		
		//exit;
        $bank = M("banks")->where(array('id'=>$map['bank_id']))->find();

		$bank_sheng=M("bank_sheng")->where($map)->find();
		$bank_sheng['status']=unserialize($bank_sheng['status']);
		//echo $bank_sheng['status'];exit;
		//dump($bank_sheng);
        if($_GET['lx'] == 0){
            $lx = '信用卡';
        }else{
            $lx = '貸款';
        }

        $this->assign('bank',$bank);
        $this->assign('lx',$lx);
		$this->assign('bank_sheng',$bank_sheng);
		$this->display();
	}
}     
